"""
Case Type   : SQL语法
Case Name   : 源表为ustore表,执行create table as
Create At   : 2023/03/31
Owner       : @zou_jialiang050
Description :
    1.开启enable_default_ustore_table
    2.创建源表
    3.create table as 创建表
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
"""

import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SQL(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.com = Common()
        self.Constant = Constant()
        self.table1 = 't_create_table_as_040_01'
        self.table2 = 't_create_table_as_040_02'
        self.enable_default_ustore_table = \
            self.com.show_param('enable_default_ustore_table')

    def test_create_table_as(self):
        text = 'text1:开启enable_default_ustore_table expect:成功'
        self.logger.info(text)
        guc_res = self.primary_sh.execute_gsguc(
            'set', self.Constant.GSGUC_SUCCESS_MSG,
            'enable_default_ustore_table=on')
        self.assertTrue(guc_res, '执行失败' + text)
        result = self.primary_sh.restart_db_cluster()
        self.assertTrue(result, '执行失败' + text)

        text = 'text2:创建源表 expect:成功'
        self.logger.info(text)
        create_table = self.primary_sh.execut_db_sql(f'''
            drop table if exists {self.table1};
            create table {self.table1}(id int);''')
        self.logger.info(create_table)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, create_table,
                      "执行失败" + text)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, create_table,
                      "执行失败" + text)

        text = 'text3:create table as 创建表 expect:成功'
        self.logger.info(text)
        create_table = self.primary_sh.execut_db_sql(
            f'''drop table if exists {self.table2};create table {self.table2} 
            as select * from {self.table1};\\d+ t_create_table_as_040_02''')
        self.logger.info(create_table)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, create_table,
                      "执行失败" + text)
        self.assertIn(self.Constant.INSERT_SUCCESS_MSG, create_table,
                      "执行失败" + text)
        self.assertIn('storage_type=ustore', create_table, "执行失败" + text)

    def tearDown(self):
        text = 'text4:清理环境 expect:成功'
        self.logger.info(text)
        clean_environment = self.primary_sh.execut_db_sql(
            f'''drop table {self.table1};drop table {self.table2};''')
        self.logger.info(clean_environment)
        guc_res = self.primary_sh.execute_gsguc(
            'set', self.Constant.GSGUC_SUCCESS_MSG,
            f'enable_default_ustore_table={self.enable_default_ustore_table}')
        self.assertTrue(guc_res, '执行失败' + text)
        result = self.primary_sh.restart_db_cluster()
        self.assertTrue(result, '执行失败' + text)
        self.assertEqual(
            clean_environment.count(self.Constant.TABLE_DROP_SUCCESS), 2,
            "执行失败" + text)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
